package com.lxl.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.lxl.dao.TiebaDAO;
import com.lxl.model.Post;
import com.lxl.model.Tieba;
import com.lxl.model.User;

/**
 * 贴吧_贴吧DAO的实现
 * 
 * @author 刘星鹭
 * @Email liuxinglumail@163.com
 * @version 创建时间： 2014年9月9日 下午4:00:58
 */
@Transactional
@Repository("tiebaDAO")
public class TiebaDAOImpl implements TiebaDAO {
    @Resource
    SessionFactory sessionFactory;// 注入会话工厂

    private String sqlFromTieba = "from Tieba";

    /**
     * 添加贴吧
     * 
     * @param tieba
     *            贴吧
     */
    @Override
    public void add(Tieba tieba) {
        sessionFactory.getCurrentSession().persist(tieba);
    }

    /**
     * 删除贴吧
     * 
     * @param tieba
     *            贴吧
     */
    @Override
    public void delete(Tieba tieba) {
        tieba.setDeleted(1);
    }

    /**
     * 恢复删除贴吧
     * 
     * @param tieba
     *            贴吧
     */
    @Override
    public void unDelete(Tieba tieba) {
        tieba.setDeleted(0);
    }

    /**
     * 更新贴吧
     * 
     * @param oldTieba
     *            旧帖吧
     * @param newTieba
     *            新贴吧
     */
    @Override
    public void update(Tieba tieba) {
        sessionFactory.getCurrentSession().merge(tieba);
    }

    /**
     * 通过贴吧名查找贴吧
     * 
     * @param tiebaName
     *            贴吧名
     * @return 贴吧
     */
    @Override
    public Tieba find(String tiebaName) {
        return (Tieba) sessionFactory.getCurrentSession().get(Tieba.class,
                tiebaName);
    }

    /**
     * 列出所有贴吧
     * 
     * @return 所有贴吧
     */
    @Override
    @SuppressWarnings("unchecked")
    public List<Tieba> list() {
        return sessionFactory.getCurrentSession().createQuery(sqlFromTieba)
                .list();
    }

    /**
     * 通过贴吧名列出某贴吧所有用户
     * 
     * @param tiebaName
     *            贴吧名
     * @return 该贴吧所有用户
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<User> listUsers(String tiebaName) {
        return sessionFactory
                .getCurrentSession()
                .createSQLQuery(
                        "select user_name from t_user_tieba where tieba_name='"
                                + tiebaName + "'").list();
    }

    /**
     * 通过贴吧名列出某贴吧所有帖子
     * 
     * @param tiebaName
     * @return 该贴吧所有帖子
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Post> listPosts(String tiebaName) {
        return sessionFactory
                .getCurrentSession()
                .createQuery("from Post where tieba_name='" + tiebaName + "'").list();
    }

    /**
     * 列出当前页面10个贴吧
     * 
     * @param start
     *            页面号
     * @return 当前页面10个贴吧
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Tieba> listTen(int start) {
        List<Tieba> list = sessionFactory.getCurrentSession()
                .createQuery("from Tieba order by tieba_name asc").list();
        if (list.size() <= 10) {
            return list;
        } else if (start == 1) {
            return list.subList(0, 10);
        } else if (list.size() - 10 * (start - 1) < 10) {
            return list.subList(10 * (start - 1), list.size());
        }
        return list.subList(10 * (start - 1), 10 * (start - 1) + 10);
    }

    /**
     * 计算总页数
     * 
     * @return 总页数
     */
    @SuppressWarnings("unchecked")
    @Override
    public int pageCount() {
        List<Tieba> list = sessionFactory.getCurrentSession()
                .createQuery(sqlFromTieba).list();
        if (list.size() % 10 == 0) {
            return list.size() / 10;
        }
        return list.size() / 10 + 1;
    }
}
